package com.hebeiwangxiaozhang.admin.controller;

import com.hebeiwangxiaozhang.admin.pojo.CommodityBrand;
import com.hebeiwangxiaozhang.admin.service.CommodityBrandService;
import com.hebeiwangxiaozhang.response.CommonResponse;
import com.hebeiwangxiaozhang.response.ResponseCode;
import com.hebeiwangxiaozhang.response.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/commodity/brand")
public class CommodityBrandController {

    @Autowired
    private CommodityBrandService commodityBrandService;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResponse createBrand(@RequestBody CommodityBrand commodityBrand) {
        try {
            commodityBrandService.create(commodityBrand);
            return ResponseUtils.okCreatedResponse();
        } catch (Exception e) {
            return ResponseUtils.failResponse(ResponseCode.BAD_REQUEST.getCode(), null, e.getMessage());
        }
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public CommonResponse deleteBrand(@PathVariable("id") Long id) {
        try {
            commodityBrandService.delete(id);
            return ResponseUtils.okDeletedResponse();
        } catch (Exception e) {
            return ResponseUtils.failResponse(ResponseCode.BAD_REQUEST.getCode(), null, e.getMessage());
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResponse updateBrand(@RequestBody CommodityBrand commodityBrand) {
        try {
            commodityBrandService.update(commodityBrand);
            return ResponseUtils.okUpdatedResponse();
        } catch (Exception e) {
            return ResponseUtils.failResponse(ResponseCode.BAD_REQUEST.getCode(), null, e.getMessage());
        }
    }

    @RequestMapping(value = "/list/{categoryId}", method = RequestMethod.GET)
    public CommonResponse getListByCategoryId(@PathVariable(value = "categoryId") Long categoryId) {
        try {
            return ResponseUtils.okResponse(commodityBrandService.getListByCategoryId(categoryId));
        } catch (Exception e) {
            return ResponseUtils.failResponse(ResponseCode.BAD_REQUEST.getCode(), null, e.getMessage());
        }
    }
}
